import {createRouter, createWebHistory} from 'vue-router'

const routes = [
    {
        path: '/',
        name: 'root',               // name可以不设置
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',              // name可以不设置
        component: () => import('@/views/Login.vue')
    },
    {
        path: '/layout',
        name: 'Layout',             // name可以不设置
        component: () => import('@/views/Layout.vue'),
        children: [
            {
                path: '/user-info',
                name: 'UserInfo',   // name可以不设置
                component: () => import('@/views/UserInfo.vue')
            },
            {
                path: '/system-settings',
                name: 'SystemSettings',   // name可以不设置
                component: () => import('@/views/SystemSettings.vue')
            }
        ]
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const isLogin = sessionStorage.getItem('isLogin') === 'true'
    if (to.path !== '/login' && !isLogin) {
        next('/login')
    } else {
        next()
    }
})

export default router